package com.ai.controller.reviewed;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ai.common.controller.BaseController;
import com.ai.common.exception.AlikAssert;
import com.ai.common.model.Pagination;
import com.ai.common.model.Result;
import com.ai.mmd.reviewed.dto.DlxxDTO;
import com.ai.mmd.reviewed.dto.QyshclDTO;
import com.ai.mmd.reviewed.dto.YhxxDTO;
import com.ai.mmd.reviewed.vo.QyshclVO;
import com.ai.mmd.reviewed.vo.YhxxVO;
import com.ai.service.reviewed.model.DlxxModel;
import com.ai.service.reviewed.model.QyshclModel;
import com.ai.service.reviewed.model.YhxxModel;
import com.ai.service.reviewed.service.IDlxxService;
import com.ai.service.reviewed.service.IQyshclService;
import com.ai.service.reviewed.service.IYhxxService;

/**
 * 会员信息审核 Created by renbangjie on 16/6/25.
 */
@RequestMapping("/yhxx/")
@Controller("com.ai.controller.reviewed.VipController")
public class YhxxController extends BaseController {

	/**
	 * 列表
	 * 
	 * @date 2016年6月26日 上午11:29:07
	 * @param yhxxDTO
	 * @return
	 * @throws Exception
	 *             Pagination<YhxxModel>
	 */
	@ResponseBody
	@RequestMapping("list")
	public Pagination<YhxxVO> list(@ModelAttribute YhxxDTO yhxxDTO)
			throws Exception {
		yhxxDTO.setSfsc(0);
		if (yhxxDTO.getSort()!=null) {
			yhxxDTO.setSort(yhxxDTO.getSort().concat(" " + yhxxDTO.getOrder()));
		}else {
			yhxxDTO.setSort("yhzt, id desc");
		}
		
		return iyhxxService.getListVO(yhxxDTO);
	}

	/**
	 * 删除
	 * 
	 * @date 2016年6月26日 下午12:39:53
	 * @param primaryKey
	 * @return
	 * @throws Exception
	 *             Result
	 */
	@ResponseBody
	@RequestMapping("delete/{primaryKey}")
	public Result delete(@PathVariable Integer primaryKey) throws Exception {
		YhxxModel yhxxModel = iyhxxService.getByPrimaryKey(primaryKey);
		AlikAssert.isNotNull(yhxxModel, "信息不存在!");
		DlxxModel dlxxModel = idlxxService
				.getOne(new DlxxDTO(yhxxModel.getYhid()));
		AlikAssert.isNotNull(dlxxModel, "用户登录信息不存在!");
		Result result = new Result(true);
		dlxxModel.setSfsc(1);
		idlxxService.updateByPrimaryKey(dlxxModel);
		result.setSuccess(true);
		return result;
	}

	/**
	 * 审核
	 * 
	 * @date 2016年6月26日 下午12:44:40
	 * @param yhxxDTO
	 * @return
	 * @throws Exception
	 *             Result
	 */
	@ResponseBody
	@RequestMapping("audit")
	public Result audit(HttpServletRequest request,
			@ModelAttribute YhxxDTO yhxxDTO) throws Exception {
		Result result = new Result(true);
		YhxxModel yhxxModel = new YhxxModel();
		BeanUtils.copyProperties(yhxxDTO, yhxxModel);
		yhxxModel.setShy(getBackendOperator(request).getId());
		yhxxModel.setShsj(new Date());
		iyhxxService.updateByPrimaryKey(yhxxModel);
		YhxxModel yhxx = iyhxxService.getByPrimaryKey(yhxxDTO.getId());
		
		String baseFolder = this.getClass().getClassLoader()
				.getResource("../../upload/pictures").getPath();
		
		System.out.println(baseFolder);
		
		File folder = new File(baseFolder+yhxx.getYhid()+"/");
		if (!folder.exists()) {
			folder.mkdir();
		}
		
		result.setSuccess(true);
		return result;
	}

	/**
	 * 详情
	 * 
	 * @date 2016年6月26日 上午11:29:24
	 * @param id
	 * @return
	 * @throws Exception
	 *             ModelAndView
	 */
	@ResponseBody
	@RequestMapping("details")
	public Pagination<QyshclVO> details(@RequestParam Integer id)
			throws Exception {
		Pagination<QyshclVO> pagination = new Pagination<QyshclVO>();
		YhxxModel yhxxModel = iyhxxService.getByPrimaryKey(id);
		if (yhxxModel == null) {
			return pagination;
		}
		YhxxVO yhxxVO = new YhxxVO();
		BeanUtils.copyProperties(yhxxModel, yhxxVO);
		List<QyshclModel> qyshclModels = iqyshclService
				.getList(new QyshclDTO(yhxxModel.getYhid()));
		List<QyshclVO> qyshclVOs = new ArrayList<QyshclVO>();
		QyshclVO qyshclVO = null;
		for (QyshclModel qyshclModel : qyshclModels) {
			qyshclVO = new QyshclVO();
			BeanUtils.copyProperties(qyshclModel, qyshclVO);
			qyshclVO.setYhxxVO(yhxxVO);
			qyshclVOs.add(qyshclVO);
		}
		pagination.setRows(qyshclVOs);
		pagination.setTotal(qyshclVOs.size());
		return pagination;
	}


	/**
	 * 修改会员等级
	 * @param yhxxDTO
	 * @return
	 * @throws Exception
     */
	@ResponseBody
	@RequestMapping("dj")
	public Result dj(@ModelAttribute YhxxDTO yhxxDTO) throws Exception{
		AlikAssert.isNotNull(yhxxDTO, "参数异常");
		AlikAssert.isNotNull(yhxxDTO.getId(), "参数异常");
		AlikAssert.isNotNull(yhxxDTO.getHydj(), "参数异常");
		yhxxDTO.setGxsj(new Date());
		Result rs = new Result(true);
		YhxxModel yhxxModel = new YhxxModel();
		BeanUtils.copyProperties(yhxxDTO, yhxxModel);
		iyhxxService.updateByPrimaryKey(yhxxModel);
		return rs;
	}


	/**
	 * 跳转
	 * 
	 * @date 2016年6月26日 上午11:29:33
	 * @param name
	 * @return
	 * @throws Exception
	 *             String
	 */
	@RequestMapping("forward/{name}")
	public String forward(@PathVariable String name) throws Exception {
		return "/reviewed/yhxx/" + name;
	}


	/**
	 * 跳转到浏览用户信息的界面
	 * @return
	 * @throws Exception
     */
	@RequestMapping("forward")
	public String forward() throws Exception{
		return "/yhxx/list";
	}
	
	/**
     * 设置推荐会员
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping("tjhy")
    public Result pwd(@ModelAttribute YhxxDTO yhxxDTO) throws Exception {
        AlikAssert.isNotNull(yhxxDTO, "参数异常.");
        AlikAssert.isNotNull(yhxxDTO.getId(), "参数异常.");
        AlikAssert.isNotNull(yhxxDTO.getYlzd2(), "参数异常.");
        Result rs = new Result(true);
        YhxxModel yhxxModel = new YhxxModel();
        BeanUtils.copyProperties(yhxxDTO, yhxxModel);
        iyhxxService.updateByPrimaryKey(yhxxModel);
        return rs;
    }

    /**
	 * 跳转到详细统计界面
	 * @return
	 * @throws Exception
     */
	@RequestMapping("forward/detailOrder")
	public String detailOrder() throws Exception{
		return "/order/detail";
	}

	@Resource(name = "iyhxxService")
	private IYhxxService iyhxxService;

	@Resource(name = "iqyshclService")
	private IQyshclService iqyshclService;

	@Resource(name = "idlxxService")
	private IDlxxService idlxxService;

}
